﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_DepartmentAuthorityDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加部门档案控制权
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static _ReturnValue Add_DepartmentAuthoriy(int departmentId, List<T_DepartmentAuthority> list)
        {
            try
            {
                ///添加部门控制时，先删除原有数据
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_DepartmentAuthority.Select(c => c).Where(c => c.DepAut_DID == departmentId);
                conn.T_DepartmentAuthority.RemoveRange(query);
                conn.SaveChanges();
                conn.T_DepartmentAuthority.AddRange(list);
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 获取部门权限
        /// </summary>
        /// <param name="departmentId"></param>
        /// <returns></returns>
        public static List<T_DepartmentAuthority> Get_DepartmentAuthority(int departmentId)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_DepartmentAuthority.Select(c => c).Where(c => c.DepAut_DID == departmentId).OrderBy(c => c.DepAut_DtoID).ToList();
            }
            catch { return null; }
        }

        /// <summary>
        /// 修改部门档案权限
        /// </summary>
        /// <param name="departmentId"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_DepartmentAuthoriy(int departmentId, List<T_DepartmentAuthority> list)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_DepartmentAuthority.Select(c => c).Where(c => c.DepAut_DID == departmentId).OrderBy(c => c.DepAut_DtoID).ToList();
                foreach(var nn in list)
                {
                    bool b = false;
                    foreach (var n in query)
                    {
                        if(n.DepAut_ID == nn.DepAut_ID)
                        {
                            n.DepAut_Approval = nn.DepAut_Approval;
                            n.DepAut_Down = nn.DepAut_Down;
                            n.DepAut_Edit = nn.DepAut_Edit;
                            n.DepAut_Print = nn.DepAut_Print;
                            n.DepAut_Show = nn.DepAut_Show;
                            n.DepAut_View = nn.DepAut_View;
                            b = true;
                            break;
                        }
                    }
                    if(!b)
                    {
                        conn.T_DepartmentAuthority.Add(nn);
                    }
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }
        
    }
}
